import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:junyeyishu/utils/ScreenFit.dart';

class QGWebViewWidget extends StatefulWidget {
  String? str;
  String? textColor;
  String? bgColor;
  QGWebViewWidget({super.key, this.str, this.textColor, this.bgColor});

  @override
  State<QGWebViewWidget> createState() => _QGWebViewWidgetState();

  InAppWebViewController? controller;
  loadData(context) {
    controller?.loadData(data: html(context));
  }

  html(str) {
    if (!kIsWeb) {
      if (Platform.isIOS && str is String && str.contains("&nbsp; ")) {
        str = str.replaceFirst("&nbsp; ", "");
      }
    }
    

    return '''<html lang="zh-CN">
                           <meta name="viewport" content="width=device-width user-scalable=no zoom=1.1">
                           <style>
                            body {
                              background: ${bgColor ?? "white"};
                              color: ${textColor ?? "#111111"};
                              font-size: ${12.rpx ?? "26"}px;
                              line-height: 26px;
                            }
                            img {max-width: 100%; height: auto}

                           </style>

                           <body>
                            <div class="container" id="_flutter_target_do_not_delete">$str</div>
                            <script>
                            function outputsize() {
                                if (typeof window.flutter_inappwebview !== "undefined" && typeof window.flutter_inappwebview.callHandler !== "undefined"){
                                window.flutter_inappwebview.callHandler('newHeight', document.getElementById("_flutter_target_do_not_delete").offsetHeight + 15);
                                }
                            }
                              new ResizeObserver(outputsize).observe(_flutter_target_do_not_delete)
                            </script>
                          </body>
                        </html>
                      ''';
  }
}

class _QGWebViewWidgetState extends State<QGWebViewWidget> {
  int heigth = 5;

  @override
  Widget build(BuildContext context) {
    if (widget.str == null || widget.str!.isEmpty) {
      return Container();
    }

    return Opacity(
      opacity: heigth == 5 ? 0.02 : 1,
      child: SizedBox(
        height: heigth.toDouble(),
        child: InAppWebView(
          onWebViewCreated: (controller) {
            widget.controller = controller;
            widget.loadData(widget.str);
            controller.reload();
          },
          // initialOptions: InAppWebViewGroupOptions(
          //   crossPlatform: InAppWebViewOptions(
          //     supportZoom: false,
          //     javaScriptEnabled: true,
          //     disableHorizontalScroll: true,
          //     disableVerticalScroll: true,
          //   ),
          // ),
          initialSettings: InAppWebViewSettings(
            supportZoom: false,
            javaScriptEnabled: true,
            disableHorizontalScroll: true,
            disableVerticalScroll: true,
          ),
          onLoadStop: ((controller, url) async {
            await Future.delayed(const Duration(milliseconds: 350));
            try {
              int? newHeight = await controller.getContentHeight();
              if (mounted && newHeight != null && newHeight != heigth) {
                heigth = newHeight;
                setState(() {});
              }
            } catch (e) {
              print(e);
            }
          }),
        ),
      ),
    );
  }
}
